<template>
  <div class="bannerNav" @mouseleave="out">
    <div class="nav_main" :class="{ active: flag }">
      <div class="nav_logo">
        <p>
          <router-link to="/">KERN</router-link>
        </p>
      </div>
      <div class="nav_contain">
        <ul>
          <li v-for="item in navList[0]" @mouseover="over(item)" :key="item.id">
            {{ item.name }}系列
          </li>
          <li>
            <router-link to="/shopmall">商城</router-link>
          </li>
          <li>
            <router-link to="">服务</router-link>
          </li>
        </ul>
      </div>
      <div class="nav_query">
        <i class="el-icon-search" @click="openSearchFn"></i>
        <div class="search_box">
          <!-- <input type="text" v-show="search" @blur="closeFn" placeholder="请输入想要搜索的商品" @keydown.enter="Searchgoods" v-model="inputValue"/> -->
          <el-select
            v-model="value"
            filterable
            remote
            reserve-keyword
            placeholder="请输入想要搜索的商品"
            :remote-method="remoteMethod"
            :loading="loading"
            @change="goParticulars"
            @blur="closeFn"
            v-show="search"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!-- <i class="el-icon-search" ></i> -->
          <p @click="closeFn" v-show="search">X</p>
        </div>
      </div>
    </div>
    <!-- 二级导航栏 -->
    <el-collapse-transition>
      <div class="navTwo" v-show="flagTwo">
        <div class="box">
          <ul>
            <li v-for="item in this.goodsInfo" :key="item.id">
              <div class="imgbox" @click="skipShopFn(item.id)">
                <img :src="item.s_goods_photos[0].path" alt="" />
              </div>
              <p>{{ item.name }}</p>
            </li>
          </ul>
          <div class="button" @click="goAllgoods">
            <ul>
              <li>
                <i class="el-icon-chat-dot-square"> 全部机型 </i>
              </li>
              <!-- <li>|</li>
              <li>
                <i class="el-icon-copy-document"
                  >对比{{ $store.state.title }}机型</i
                >
              </li> -->
            </ul>
          </div>
        </div>
      </div>
    </el-collapse-transition>
  </div>
</template>

<script>
import { Shoptext, adminLogin, Searchgoods } from "../api/test.js";
export default {
  name: "navTwo",
  async created() {
    if (!sessionStorage.getItem("token")) {
      await adminLogin({
        phone: 13242998652,
        password: 123456,
      })
        .then((res) => {
          sessionStorage.setItem("token", res.data.result.token);
        })
        .catch((err) => {
          console.log(err);
        });
    }
    if (!sessionStorage.getItem("headerClassify")) {
      await Shoptext({
        project_id: 149,
      }).then((res) => {
        console.log(res);
        sessionStorage.setItem(
          "headerClassify",
          JSON.stringify(res.data.result[0].children)
        );
        sessionStorage.setItem(
          "shopClassify",
          JSON.stringify(res.data.result[2].children)
        );
        this.$store.commit("leftnav", res.data.result[2].children);
        this.navList.push(res.data.result[0].children);

        // console.log(res.data.result[2].children);
      });
    } else {
      console.log(JSON.parse(sessionStorage.getItem("headerClassify")));
      this.navList.push(JSON.parse(sessionStorage.getItem("headerClassify")));
    }
    console.log(1);
  },
  data() {
    return {
      flag: false,
      flagTwo: false,
      top: 0,
      navList: [],
      navName: "",
      search: false,
      inputValue: "",
      options: [],
      value: [],
      list: [],
      loading: false,
      goodsInfo: [],
    };
  },
  async mounted() {
    window.addEventListener("scroll", this.handleScroll, true);

    console.log(2);
  },
  methods: {
    goAllgoods() {
      this.$router.push({ path: "/Allgoods" });
    },
    goParticulars(data) {
      console.log(data);
      let res = this.options.find((item) => {
        return item.value == data;
      });
      console.log(res);
      this.skipShopFn(res.id);
      // this.$router.push({path:`/particulars/${res.id}`})
    },
    async remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        let res = await Searchgoods({
          name: query,
          project_id: 149,
        });
        console.log(res.data);
        this.list = res.data.result.rows.map((item) => {
          return { id: `${item.id}`, value: `${item.name}` };
        });
        setTimeout(() => {
          this.loading = false;
          // this.options = this.list.filter(item => {
          //   return item.value.indexOf(query) > -1;
          // });
          // console.log(this.options);
          this.options = this.list;
          console.log(this.options);
        }, 200);
      } else {
        this.options = [];
      }
    },
    handleScroll() {
      this.top = window.scrollY;
      if (this.top > 40) {
        this.flag = true;
      } else {
        this.flag = false;
      }
    },
    over(item) {
      this.flagTwo = true;
      this.flag = true;
      // console.log(item);
      this.goodsInfo = item.s_goods;
      // if (JSON.parse(sessionStorage.getItem(item.name))) {
      //   this.$store.state.navTwoList = JSON.parse(
      //     sessionStorage.getItem(item.name)
      //   );
      //   this.$store.state.title = item.name;
      // } else {
      //   Shopimg({
      //     project_id: 149,
      //     classify_id: item.id,
      //   }).then((res) => {
      //     // console.log(res);
      //     this.$store.state.navTwoList = res.data.result.rows;
      //     this.$store.state.title = item.name;
      //     sessionStorage.setItem(
      //       item.name,
      //       JSON.stringify(res.data.result.rows)
      //     );
      //   });
      // }
    },
    out() {
      this.flagTwo = false;
    },

    skipShopFn(id) {
      this.$router.push({ path: `/particulars/${id}`, redirect: true });
      // if(this.$route)
      if (this.$route.name == "Particulars") {
        window.location.reload();
      }
    },

    openSearchFn() {
      this.search = true;
    },

    closeFn() {
      this.search = false;
    },
  },
};
</script>

<style lang="scss">
.navTwo {
  z-index: 999;
  width: 100%;
  height: 350px;
  background-color: #fff;
  position: absolute;
  top: 80px;
  .box {
    ul {
      height: 60%;
      display: flex;
      justify-content: center;
      text-align: center;
      li {
        .imgbox {
          margin: 30px 20px;
          background-color: #f7f8fa;
          border-radius: 20%;
          width: 150px;
          height: 150px;
          box-sizing: border-box;
          padding: 10% 0;
          img {
            height: 100%;
          }
        }
      }
    }
  }

  .button {
    background-color: #f7f8fa;
    width: 300px;
    height: 50px;
    margin: 0 auto;
    line-height: 50px;
    border-radius: 100px;
    transition: 0.5s;
    ul {
      margin-top: 30px;
      display: flex;
      justify-content: space-around;
    }
    &:hover {
      background-color: black;
      color: white;
      cursor: pointer;
    }
  }
}
.bannerNav {
  z-index: 999;
  position: sticky;
  top: 0;
}
.nav_main {
  padding: 0 5%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // width: 100%;
  height: 80px;
  background-color: transparent;
  transition: all 1s;
  .nav_logo {
    p {
      a {
        font-size: 40px;
        text-decoration: none;
        color: #000;
        font-weight: 700;
      }
    }
    p:hover {
      a {
        color: #415fff;
        transition: all 0.5s;
      }
    }
  }
  .nav_contain {
    width: 55%;
    ul {
      display: flex;
      justify-content: space-between;
      li {
        cursor: pointer;
        line-height: 80px;
        color: black;
        text-decoration: none;
        font-size: 14px;
        a {
          color: black;
          text-decoration: none;
          font-size: 14px;
        }
      }
      li:hover {
        a {
          color: #415fff;
          transition: all 0.5s;
        }
      }
    }
  }
  .nav_query {
    position: relative;
    display: flex;
    align-items: center;
    margin-right: 50px;
    font-size: 20px;
    cursor: pointer;
    :hover {
      color: #415fff;
      transition: all 0.5s;
    }
    .search_box {
      position: absolute;
      display: flex;
      align-items: center;
      left: -200px;
      input {
        display: inline-block;
        margin: 0 20px 0 10px;
        height: 25px;
        width: 230px;
        border-radius: 5px;
        padding-left: 20px;
        transition: all 0.5s;
        outline: none;
        border: none;
        // background-color: transparent;
        // box-sizing: border-box;
      }
      p {
        font-size: 14px;
      }
    }
  }
}
.active {
  background-color: white;
}
</style>